package com.gao.controller;

import com.gao.pojo.Payment;
import com.gao.entities.Result;
import com.gao.service.IPaymentService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: suGar
 * @Date: 2022/10/13 9:39
 */
@RestController
@RequestMapping("/payment")
public class PaymentController {



    IPaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;
    @Autowired
    public void setPaymentService(IPaymentService paymentService) {
        this.paymentService = paymentService;
    }

    @GetMapping("{id}")
    public Result getPayment(@PathVariable("id") Integer id) {
        Payment payment = paymentService.getById(id);
        if (payment != null) {
            return Result.success("获取成功:" + serverPort, payment);
        }
        return Result.success("暂无数据:" + serverPort);
    }

    @GetMapping
    public Result getAllPayment() {
        return Result.success("获取成功:" + serverPort, paymentService.list());
    }

    @PostMapping
    public Result addPayment(@RequestBody Payment payment) {
        if (paymentService.saveOrUpdate(payment)) {
            return Result.success("添加成功:" + serverPort);
        }
        return Result.fail("添加失败:" + serverPort);
    }

}
